package com.artisan.provider.controller;

import com.artisan.provider.covtransformation.CovJurisdictionTransformation;
import com.artisan.provider.domain.ZgUserJurisdiction;
import com.artisan.provider.domain.ZgUserMenu;
import com.artisan.provider.dto.ZgUserParamDto;
import com.artisan.provider.po.ZgUserMenuPo;
import com.artisan.provider.service.ZgUserJurisdictionService;
import com.artisan.provider.vo.ZgUserJurisdictionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ZgUserJurisdictionController {

    @Autowired
    ZgUserJurisdictionService zgUserJurisdictionService;


    @RequestMapping(value = "/api/jurisdiction/list")
    public List<ZgUserJurisdictionVo> jurisdictionList(){
        List<ZgUserJurisdiction> zgUserJurisdictions = zgUserJurisdictionService.jurisdictionList();
        List<ZgUserJurisdictionVo> zgIntegralVos = CovJurisdictionTransformation.INSTANCE.rPageVo(zgUserJurisdictions);

        List<ZgUserJurisdictionVo> zgUserJurisdictionVos = new ArrayList<>();

        for (ZgUserJurisdictionVo zgIntegralVo : zgIntegralVos) {
            ZgUserJurisdictionVo zgUserJurisdictionVo = new ZgUserJurisdictionVo();
            ZgUserJurisdictionVo zgIntegralVo1 = zgIntegralVo;
            zgUserJurisdictionVo = zgIntegralVo1;

            if (zgIntegralVo1.getMenuList() != null){
                List<String> stringList = new ArrayList();
                for (String s : zgIntegralVo1.getMenuList().split(",")) {
                    stringList.add(s);
                }
                zgUserJurisdictionVo.setMenuListString(stringList);
            }
            zgUserJurisdictionVos.add(zgUserJurisdictionVo);
        }

        return zgUserJurisdictionVos;
    }


    /**
     * 获取菜单列表
     * @param zgUserParamDto
     * @return
     */
    @PostMapping(value = "/api/jurisdiction/menu")
    public List<ZgUserMenu> juisdictionMenu(@RequestBody(required = false) ZgUserParamDto zgUserParamDto){
        List<String> str = new ArrayList<>();
        if(zgUserParamDto.getMenuLists() == null){
            return new ArrayList<>();
        }
        for (String s : zgUserParamDto.getMenuLists().split(",")) {
            str.add(s);
        }

        zgUserParamDto.setMenuList(str);
        return zgUserJurisdictionService.juidictionMenu(zgUserParamDto);

    }

    /**
     * 获取path路径
     */
    @PostMapping(value = "/api/jurisdiction/menu/path")
    public List<String> jurisdictionMenuPath(@RequestBody(required = false)ZgUserParamDto zgUserParamDto){
        List<String> str = new ArrayList<>();
        if(zgUserParamDto.getMenuLists() == null){
            return new ArrayList<>();
        }
        for (String s : zgUserParamDto.getMenuLists().split(",")) {
            str.add(s);
        }

        zgUserParamDto.setMenuList(str);
        return zgUserJurisdictionService.juidictionMenuPath(zgUserParamDto);
    }

    /**
     * 获取菜单
     * @return
     */
    @GetMapping(value = "/api/jurisdiction/menu/list")
    public List<ZgUserMenuPo> jurisdictionMenuList(){
        return zgUserJurisdictionService.jurisdictionMenuList();
    }


    /**
     * 新增用户权限
     * @param zgUserJurisdiction
     * @return
     */
    @PostMapping(value = "/api/add/jurisdiction/menu")
    public int addJurisdictionMenu(@RequestBody ZgUserJurisdiction zgUserJurisdiction){


        zgUserJurisdiction.setCreateTime(System.currentTimeMillis()+"");
        int i = zgUserJurisdictionService.addJurisdictionMenu(zgUserJurisdiction);
        return 0;
    }

    /**
     * 修改权限
     * @param zgUserJurisdiction
     * @return
     */
    @PutMapping(value = "/api/update/jurisdiction/menu")
    public int updateJurisdictionMenu(@RequestBody ZgUserJurisdiction zgUserJurisdiction){
        zgUserJurisdiction.setUpdateTime(System.currentTimeMillis()+"");
        int i = zgUserJurisdictionService.updateJurisdictionMenu(zgUserJurisdiction);
        return 0;
    }


}
